//  @ Project : GagalPanen
//  @ File Name : gamemaps.cpp
//  @ Date : 3/9/2011
//  @ Author : 
//
//

#include <assert.h>
#include "gamemaps.hpp"
#include "fieldconstants.hpp"
#include "houseconstants.hpp"
#include "shopconstants.hpp"

GameMaps::GameMaps(GameInterface* game) :
    maps_() {
  maps_.push_back(new GridMap(game, HouseConstants::getMap()));
  maps_.push_back(new GridMap(game, FieldConstants::getMap()));
  maps_.push_back(new GridMap(game, ShopConstants::getMap()));
}

GameMaps::GameMaps(GameInterface* game, std::ifstream& is) :
    maps_() {
  int n;
  is >> n;
  for (int i = 0; i < n; ++i) {
    maps_.push_back(new GridMap(game, is));
  }
}

void GameMaps::save(std::ofstream& os) {
  os << maps_.size() << std::endl;
  for (int i = 0; i < (int)maps_.size(); ++i) {
    maps_[i]->save(os);
  }
}

Cell* GameMaps::getCellInFrontOff(Cell* cell, Direction dir) {
  if (getField()->containsCell(cell)) {
    return getField()->getCellInFrontOff(cell, dir);
  } else if (getHouse()->containsCell(cell)) {
    return getHouse()->getCellInFrontOff(cell, dir);
  } else if (getShop()->containsCell(cell)) {
    return getShop()->getCellInFrontOff(cell, dir);
  }
  assert(false);
}

GridMap* GameMaps::getField() {
  return maps_[1];
}

GridMap* GameMaps::getHouse() {
  return maps_[0];
}

GridMap* GameMaps::getShop() {
  return maps_[2];
}

GridMap* GameMaps::getMapContaining(Cell* cell) {
  if (getField()->containsCell(cell)) {
    return getField();
  } else if (getShop()->containsCell(cell)) {
    return getShop();
  } else if (getHouse()->containsCell(cell)) {
    return getHouse();
  }
  assert(false);
}

void GameMaps::advanceTime() {
  getField()->advanceTime();
  getShop()->advanceTime();
  getHouse()->advanceTime();
}
void GameMaps::advanceDay() {
  getField()->advanceDay();
  getShop()->advanceDay();
  getHouse()->advanceDay();
}

std::string GameMaps::getRepFromMap(GridMap* map) {
  if (map == getField()) {
    return "FIELD";
  } else if (map == getShop()) {
    return "SHOP";
  } else if (map == getHouse()) {
    return "HOUSE";
  } else {
    assert(false);
  }
}
GridMap* GameMaps::getMapFromRep(std::string rep) {
  if (rep == "FIELD") {
    return getField();
  } else if (rep == "SHOP") {
    return getShop();
  } else if (rep == "HOUSE") {
    return getHouse();
  } else {
    assert(false);
  }
}

